package com.bw.Threads.pattern;

import java.util.List;

public class Producer implements Runnable{

    private List<String>  backet;

    private Integer maxSize;

    public Producer(List<String> backet,Integer maxSize){
        this.backet = backet;
        this.maxSize = maxSize;
    }

    @Override
    public void run() {
        while (true){
            synchronized (backet) {
                while (backet.size() == maxSize) {
                    System.out.println("缓冲区已经满了");
                    try {
                        backet.wait();//线程等待
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //生产  等待1秒
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                backet.add("包子");//生产一个
                System.out.println(Thread.currentThread().getName()+" ,当期包子个数:"+backet.size());
                backet.notifyAll();//生产了之后就通知其它线程
                try {
                    backet.wait();//本线程等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
